package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.entity.SmsAdmin;
import com.caiyi.financial.nirvana.sms.querybean.AdminQueryBean;
import com.caiyi.financial.nirvana.sms.vo.SmsAdminVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface SmsAdminMapper extends BaseMapper<SmsAdmin> {

    @SelectProvider(type = AdminSqlProvider.class, method = "selectAllAdminByParams")
    List<SmsAdminVo> getAllAdmin(AdminQueryBean bean);

    class AdminSqlProvider{
        public String selectAllAdminByParams(AdminQueryBean bean){
            return new SQL(){
                {
                    SELECT("a.id,a.account,a.password,a.phone,r.name as roleName,a.create_time as createTime,a.update_time as" +
                            "updateTime,a.name,a.role_id as roleId");
                    FROM("sms_admin a");
                    LEFT_OUTER_JOIN("sms_role r on a.role_id=r.id");
                    if (!StringUtils.isEmpty(bean.getAccount())) {
                        WHERE("a.account LIKE concat('%',#{account},'%')");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("a.create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("a.create_time <=date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");
                }
            }.toString();
        }
    }

    @Select("select * from (SELECT * FROM sms_admin UNION ALL SELECT b.id,b.account,b.password,b.phone,'3' as role_id,b.create_time,null as update_time,null as name FROM sms_user b) m  where account =#{account} limit 1 ")
    SmsAdmin selectAccount(String account);
}
